import {
  asyncRouterMap,
  constantRouterMap
} from '@/config/router.config'
const asyncRouterMap2 = deepClone(asyncRouterMap)
const asyncRouterMap3 = deepClone(asyncRouterMap)
/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
// function hasPermission (permission, route) {
//   if (route.meta && route.meta.permission) {
//     let flag = false
//     for (let i = 0, len = permission.length; i < len; i++) {
//       flag = route.meta.permission.includes(permission[i])
//       if (flag) {
//         return true
//       }
//     }
//     return false
//   }
//   return true
// }

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
/*function hasRole(roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id)
  } else {
    return true
  }
}

function filterAsyncRouter (routerMap, roles) {
  const accessedRouters = routerMap.filter(route => {
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}*/
function hasPermission(roles, route) {
  if (route.meta && route.meta.code) {
    return roles.indexOf(route.meta.code) >= 0
  } else {
    return true
  }
}

function deepClone(source) {
  const targetObj = source.constructor === Array ? [] : {}; // 判断复制的目标是数组还是对象
  for (let keys in source) { // 遍历目标
    if (source.hasOwnProperty(keys)) {
      if (source[keys] && typeof source[keys] === 'object') { // 如果值是对象，就递归一下
        targetObj[keys] = source[keys].constructor === Array ? [] : {};
        targetObj[keys] = deepClone(source[keys]);
      } else { // 如果不是，就直接赋值
        targetObj[keys] = source[keys];
      }
    }
  }
  return targetObj;
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, roles) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(roles, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

function getAllPath(asyncRouterMap, allpath) {
  asyncRouterMap.forEach((item) => {
    allpath.push(item.path)
    if (item.children && item.children.length) {
      getAllPath(item.children, allpath)
    }
  })
  return allpath
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    allPath: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    },
    SET_ALLPATH: (state, allPath) => {
      state.allPath = allPath
    }
  },
  actions: {
    GenerateRoutes({
      commit
    }, data) {
      return new Promise(resolve => {
        const {
          roles
        } = data
        const accessedRouters = filterAsyncRouter(asyncRouterMap2, roles)
        commit('SET_ROUTERS', accessedRouters)
        const allpath = getAllPath(asyncRouterMap3, [])
        commit('SET_ALLPATH', allpath)
        resolve()
      })
    }
  }
}

export default permission
